package com.bjsxt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.constants.Constants;
import com.bjsxt.domain.Registration;
import com.bjsxt.dto.RegistrationDto;
import com.bjsxt.mapper.RegistrationMapper;
import com.bjsxt.service.RegistrationService;
import com.bjsxt.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 门诊挂号service接口实现类
 */
@Service
public class RegistrationServiceImpl implements RegistrationService {

    @Autowired
    private RegistrationMapper registrationMapper;

    /**
     * 保存挂号单信息
     *
     * @param registrationDto
     */
    @Override
    public void addRegistration(RegistrationDto registrationDto) {
        Registration registration = new Registration();
        BeanUtil.copyProperties(registrationDto, registration);
        registration.setRegistrationStatus(Constants.REG_STATUS_0);
        registration.setCreateBy(registrationDto.getSimpleUser().getUserName());
        registration.setCreateTime(DateUtil.date());
        this.registrationMapper.insert(registration);
    }

    /**
     * 分页加载挂号列表【默认为当天】
     *
     * @param registrationDto
     * @return
     */
    @Override
    public DataGridView queryRegistrationForPage(RegistrationDto registrationDto) {
        Page<Registration> page = new Page<>(registrationDto.getPageNum(), registrationDto.getPageSize());
        QueryWrapper<Registration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(registrationDto.getDeptId() != null, Registration.COL_DEPT_ID, registrationDto.getDeptId());
        queryWrapper.eq(StringUtils.isNotBlank(registrationDto.getSchedulingType()), Registration.COL_SCHEDULING_TYPE, registrationDto.getSchedulingType());
        queryWrapper.eq(StringUtils.isNotBlank(registrationDto.getSubsectionType()), Registration.COL_SUBSECTION_TYPE, registrationDto.getSubsectionType());
        queryWrapper.eq(StringUtils.isNotBlank(registrationDto.getRegStatus()), Registration.COL_REGISTRATION_STATUS, registrationDto.getRegStatus());
        queryWrapper.eq(StringUtils.isNotBlank(registrationDto.getVisitDate()), Registration.COL_VISIT_DATE, registrationDto.getVisitDate());
        queryWrapper.ge(registrationDto.getBeginTime() != null, Registration.COL_VISIT_DATE, DateUtil.format(registrationDto.getBeginTime(), "yyyy-MM-dd"));
        queryWrapper.le(registrationDto.getEndTime() != null, Registration.COL_VISIT_DATE, DateUtil.format(registrationDto.getEndTime(), "yyyy-MM-dd"));
        queryWrapper.orderByDesc(Registration.COL_CREATE_TIME);
        this.registrationMapper.selectPage(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 更新挂号单信息
     *
     * @param registration
     * @return
     */
    @Override
    public int updateRegistrationById(Registration registration) {
        return this.registrationMapper.updateById(registration);
    }

    /**
     * 根据挂号id查询挂号信息
     *
     * @param registrationId
     * @return
     */
    @Override
    public Registration queryRegistrationByRegId(String registrationId) {
        return this.registrationMapper.selectById(registrationId);
    }

    /**
     * 根据条件查询挂号信息
     *
     * @param deptId
     * @param subsectionType
     * @param scheudlingType
     * @param regStatus
     * @param userId
     * @return
     */
    @Override
    public List<Registration> queryRegistration(Long deptId, String subsectionType, String scheudlingType, String regStatus, Long userId) {
        QueryWrapper<Registration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Registration.COL_DEPT_ID, deptId);
        queryWrapper.eq(StringUtils.isNotBlank(subsectionType), Registration.COL_SUBSECTION_TYPE, subsectionType);
        queryWrapper.eq(Registration.COL_SCHEDULING_TYPE, scheudlingType);
        queryWrapper.eq(Registration.COL_REGISTRATION_STATUS, regStatus);
        queryWrapper.eq(Registration.COL_VISIT_DATE, DateUtil.format(DateUtil.date(), "yyyy-MM-dd"));
        queryWrapper.eq(null != userId, Registration.COL_USER_ID, userId);
        queryWrapper.orderByAsc(Registration.COL_REGISTRATION_NUMBER);
        return this.registrationMapper.selectList(queryWrapper);
    }
}
